const http = require("../../../../utils/request");
Page({
  /**
   * 页面的初始数据
   */
  data: {
    loaded: false,
    enable: false,
    loadingProps: {
      size: '40rpx',
    },
    backTopVisible: false,
    amount:100,
    fenbo:0,
    breed:0,
    change_data:{},
    operation: 0,
    value:"",
    count: 0,
    imgs:[],
    dateTwoVisible: false,
    dateTwo: new Date().getTime(), // 支持时间戳传入
    // 指定选择区间起始值
    start: "2000-01-01",
    end: "2030-09-09",
    fabButton: {
      icon: 'add',
      openType: 'add',
      theme: 'light',
    },
    trans: [],
    type:{
      destination: "货品去向",
      count:"分拨数量",
      driver:"运输司机",
      licenseNumber: "车牌号码",
      arrivalTime: "送达时间"
    }
  },
  change(v) {
    let index = v.currentTarget.dataset.data
    let data = this.data.change_data
    data[index] = v.detail.value
    this.setData({
      data,
    })
  },
  cancel() {
    this.setData({
      operation:0,
    })
  },
  confirm() {
    http.putRequest('/admin/dispatch',JSON.stringify(this.data.change_data),res=>{
      wx:wx.showToast({
        title: '修改成功',
      })
      this.onLoad()
    },err=>{})
    this.cancel()
  },
  handleChange(e) {
    const { value } = e.detail;
    this.setData({
      count:value,
    });
    this.change(e);
  },
  showPickerTwo(e) {
    const { mode } = e.currentTarget.dataset;
    this.setData({
      mode,
      [`dateTwoVisible`]: true,
    });
  },
  hidePickerTwo() {
    const { mode } = this.data;
    this.setData({
      [`dateTwoVisible`]: false,
    });
  },
  onConfirmTwo(e) {
    const { value } = e.detail;
    const { mode } = this.data;
    console.log("confirm", value);
    let data = this.data.change_data
    data['arrivalTime'] = value
    this.setData({
      [mode]: value,
      data,
      value:value
    });
    this.hidePickerTwo();
  },
  onColumnChangeTwo(e) {
    console.log("pick", e.detail.value);
  },
  onUpload(file) {
    const { imgs } = this.data;
    const { length } = imgs;
    this.setData({
      imgs: [...imgs,{...file,status:'loading'}],
      display: true
    });
    const task = wx.uploadFile({
      url: 'https://37y5825u15.oicp.vip/admin/cos/upload', // 仅为示例，非真实的接口地址
      filePath: file.url,
      name: 'file',
      formData: { user: 'test' },
      success: (res) => {
        this.setData({
          [`imgs[${length}].status`]: 'done',
          display: false,
        });
        this.setData({
          [`datas.picture[${length}]`]: JSON.parse(res.data).data,
        });
      },
    });
    task.onProgressUpdate((res) => {
      this.setData({
        [`imgs[${length}].percent`]: res.progress,
      });
    });
  },
  handleSuccess(e) {
    const { files } = e.detail;
    this.onUpload(files[files.length-1]);
  },
  handleRemove(e) {
    const { index } = e.detail;
    const { imgs } = this.data;
    imgs.splice(index, 1);
    let data = this.data.change_data
    data['picture'].splice(index,1)
    this.setData({
      imgs,
      data
    });
  },
  handleClick(e) {
    console.log(e.detail.file);
  },
  add() {
    wx:wx.navigateTo({
      url: '../../add_trans/add_trans',
      events: null,
      success: (result) => {
        console.log(result);
      },
      fail: (res) => {},
      complete: (res) => {},
    })
  },
  onEdit(v) {
    let imgs = [];
    if(v.currentTarget.dataset.data['picture']) {
      for(let i = 0;i<v.currentTarget.dataset.data['picture'].length;i++) {
        imgs.push({
          name: v.currentTarget.dataset.data['picture'][i],
          url: v.currentTarget.dataset.data['picture'][i],
          type: 'image'
        })
      }
    }
    this.setData({
      operation: 1,
      change_data:v.currentTarget.dataset.data,
      value: v.currentTarget.dataset.data['arrivalTime'],
      count: v.currentTarget.dataset.data['count'],
      imgs: imgs
    })
  },
  load(){
    let data = getApp().globalData;
    let breed = data.codeing;
    let datas = []
    http.getRequest("/admin/cultivation/"+breed+"/dispatch",{},res=>{
      datas = res.data.data
      this.setData({
        trans: datas,
        breed: breed
      })
    },err=>{})
    http.getRequest("/admin/cultivation/"+breed+"/residue",{},res=> {
      this.setData({
        fenbo: res.data.data,
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.load()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.load()
  },
  onRefresh() {
    new Promise((res,rej)=>{
      setTimeout(()=>{
        this.setData({
          loaded:true
        })
        res();
      },0);
    }).then((res)=>{
      setTimeout(()=>{
        this.setData({
          enable:true
        })
      },10)
    }).then(()=>{
      this.load();
    })
    .then(()=>{
      setTimeout(()=>{
        this.setData({
          enable:false,
        })
      },100);
    })
    .then((res)=>{
      setTimeout(()=>{
        this.setData({
          loaded:false
        })
      },500)
    });
  },
  onScroll(e) {
    const { scrollTop } = e.detail;
    this.setData({
      backTopVisible: scrollTop > 100,
    });
  },
})